//
//  LocalStorage.m
//  StudentClient
//
//  Created by Shadow on 14-4-22.
//  Copyright (c) 2014年 imobile. All rights reserved.
//

#import "LocalStorage.h"
#import "StringUtil.h"
#import "HYFileManager.h"

//http://api.lirenkj.com
//http://192.168.50.251:5080
//http://192.168.2.129:8083/sdk
//http://192.168.50.251:4070
NSString * const defaultHttpUrl = @"http://122.114.22.68:4060";

NSString * const defaultName = @"西安开元合众机电有限公司";


int const teacherport = 60000;

@implementation LocalStorage

- (id)init {
    if (self = [super init]) {
        NSString *paths = [HYFileManager documentsDir];
        _rootPath = [paths stringByAppendingPathComponent:APP_DOCUMENT_PATH];
        if ([[NSFileManager defaultManager] fileExistsAtPath:_rootPath] == NO) {
            [self createDirectory:_rootPath];
        }
        _configPath=[paths stringByAppendingPathComponent:APP_DOCUMENT_Config_PATH];
        if ([[NSFileManager defaultManager] fileExistsAtPath:_configPath] == NO) {
            [self createDirectory:_configPath];
        }
        DDLogError(@"%@",_rootPath);
    }
    return self;
}

#pragma mark - 创建文件夹
- (BOOL)createDirectory:(NSString *)path {
    if ([[NSFileManager defaultManager] fileExistsAtPath:path] == NO) {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil]) {
            return NO;
        }
    }
    return YES;
}


/**
 *  获取本地网络配置文件
 *
 *  @return 本地网络配置文件地址
 */
- (NSString *)getLocalHttpAndNamePath{
    NSString *path = [_configPath stringByAppendingPathComponent:@"HttpConfigAndName"];
    if ([self createDirectory:path]) {
        return [path stringByAppendingPathComponent:@"HttpConfigAndName.plist"];
    }
    return nil;
}

/**
 *  保存网络配置
 *
 *  @param httphost  云平台根地址
 *  @param name 教师端请求ip地址
 *
 *  @return 是否保存成功
 */

- (BOOL)saveLocalHttpConfig:(NSString*)httphost andGasSupplierName:(NSString*)name{
    NSString * platformpath  = [self getLocalHttpAndNamePath];
    if (![StringUtil isEmpty:httphost needTrim:YES]&&![StringUtil isEmpty:name needTrim:YES]) {
         NSDictionary* userdic = @{@"HttpConfig":httphost,@"Name":name};
         [userdic writeToFile:platformpath atomically:YES];
        return YES;
    }else{
        return NO;
    }
    
}

/**
 *  获取服务器地址
 *
 *  @return 服务器地址
 */

- (NSString*)getHttpConfig{
    NSString * platformpath  = [self getLocalHttpAndNamePath];
    NSDictionary * http = [NSDictionary dictionaryWithContentsOfFile:platformpath];
    return  http[@"HttpConfig"]==nil?defaultHttpUrl: http[@"HttpConfig"];
}

/**
 *  获取天然气公司的名称
 *
 *  @return 天然气公司的名称
 */

- (NSString*)getName{
    NSString * platformpath  = [self getLocalHttpAndNamePath];
    NSDictionary * http = [NSDictionary dictionaryWithContentsOfFile:platformpath];
    return  http[@"Name"]==nil?defaultName: http[@"Name"];

}

#pragma mark- 将照片保存到沙盒
+ (void)saveImageToSandbox:(UIImage *)image andImageNage:(NSString *)imageName andResultBlock:(resultBlock)block{
    
    //高保真压缩图片，此方法可将图片压缩，但是图片质量基本不变，第二个参数为质量参数
    NSData *imageData=UIImageJPEGRepresentation(image, 0.5);
   
    NSString *filePath=[self filePath:imageName];
    BOOL result=[imageData writeToFile:filePath atomically:YES];
    //保存成功传值到blcok中
    if (result) {
        block(result);
    }
}

#pragma mark- 删除头像
+ (void)deleteAvatarImage:(NSString *)imageName{
    
    NSString *filePath=[self filePath:imageName];
    NSFileManager * file = [NSFileManager defaultManager];
    [file removeItemAtPath:filePath error:nil];
}

#pragma mark- 获取沙盒路径
+ (NSString *)filePath:(NSString *)fileName{
    
    //获取沙盒目录
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //保存文件名称
    NSString *filePath=[paths[0] stringByAppendingPathComponent:fileName];
    
    return filePath;
}

#pragma mark- 从沙盒中读取照片
+ (UIImage *)loadImageFromSandbox:(NSString *)imageName{
    //获取沙盒路径
    NSString *filePath=[self filePath:imageName];
    //根据路径读取image
    UIImage *image=[UIImage imageWithContentsOfFile:filePath];
    
    return image;
}

/**
 *  获取本地用户文件
 *
 *  @return 本地用户文件地址
 */
- (NSString *)getLocalUserInfoPath{
    
    NSString *path = [_configPath stringByAppendingPathComponent:@"UserInfo"];
    if ([self createDirectory:path]) {
        return [path stringByAppendingPathComponent:@"userInfo.plist"];
    }
    return nil;
}

#pragma mark- 保存用户信息
- (BOOL)saveUserInfo:(NSDictionary *)userInfo{
    
    NSString * userPath  = [self getLocalUserInfoPath];
    if (userInfo!=nil) {
        if ([userInfo writeToFile:userPath atomically:YES]) {
            return YES;
        }else{
            return NO;
        }
    }else{
        return NO;
    }
    
}

#pragma mark- 获取用户信息
//- (UserInfoModel *)getUserInfo{
//    NSString *userPath  = [self getLocalUserInfoPath];
//    
//    UserInfoModel *userInfo = [[UserInfoModel alloc]init];
//    [userInfo setValuesForKeysWithDictionary:[NSDictionary dictionaryWithContentsOfFile:userPath]];
//    return userInfo ;
//}
@end
